Skip to content

Add Deep Core Style#4925

Open
cloutiertyler wants to merge 4 commits intomasterfrom
tyler/programming-standards
Open

Add Deep Core Style#4925
cloutiertyler wants to merge 4 commits intomasterfrom
tyler/programming-standards

Conversation

@cloutiertyler
Copy link
Copy Markdown
Contributor

@cloutiertyler cloutiertyler commented Apr 30, 2026

Summary

Adds docs/DEEP_CORE_STYLE.md, a style guide for the core of SpacetimeDB (datastore, commitlog, snapshotting, replication).

Read the rendered document

The document is organized around seven principles:

  1. Work towards zero dependencies
  2. Work towards deterministic simulation testing
  3. Work towards thread-per-core
  4. Work towards no_std
  5. Think in terms of persistent data structures
  6. Think in terms of pipelining
  7. Think in terms of unreliable processes

A short style section follows the principles, covering assertions, bounded loops and queues, error handling, control flow, naming, and formatting. Inspired by TIGER STYLE, narrowed and adapted for Rust and our principles.

This is a seed document. It will grow as we make the principles operational in code and as the practices that serve them become clearer with use.

Test plan

  • Read the document and review the principles
  • Discuss any principle the team disagrees with before merging

Replaces SPACETIME_PROGRAMMING_STANDARDS.md with DEEP_CORE_STYLE.md.
The document is reorganized around seven principles for the core
(datastore, commitlog, snapshotting, replication):

1. Work towards zero dependencies
2. Work towards deterministic simulation testing
3. Work towards thread-per-core
4. Work towards no_std
5. Think in terms of persistent data structures
6. Think in terms of pipelining
7. Think in terms of unreliable processes

A short style section follows the principles, covering assertions,
bounded loops and queues, error handling, control flow, naming, and
formatting.
@cloutiertyler cloutiertyler requested a review from aasoni May 1, 2026 14:39
Comment thread docs/DEEP_CORE_STYLE.md

## 4. Work towards `no_std`

To control our failure modes, we should enforce no memory allocation inside the core. This is not absolute. Primitives like pages can be allocated outside the core and passed in. But the rule is that the deep core does not allocate.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the natural way to do this is to be generic over an allocator trait. Otherwise, this is probably going to be very intrusive in the datastore.

Comment thread docs/DEEP_CORE_STYLE.md

## 5. Think in terms of persistent data structures

We want to support time-travel APIs, sub-transactions, background snapshotting, and potentially MVCC. Persistent data structures, such as Merkle trees and Postgres-style MVCC, naturally allow us to look at multiple versions of data and update versions atomically.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We are deeply committed to mutable non-persistent structures in the datastore right now.)

Comment thread docs/DEEP_CORE_STYLE.md

We should model the core's communication with the outside world (Tokio, disk I/O, networking, peers) as unreliable, asynchronous message passing.

This sharpens our error handling. Every message can be lost, delayed, or reordered, and the core's logic must remain correct under those conditions. It is also a natural fit with principle 6, since messages to other processes are inherently pipelined.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about corruption and protection against cosmic rays?

Comment thread docs/DEEP_CORE_STYLE.md

- Every loop has a static upper bound. If a loop must not terminate (an event loop, for example), that fact is itself asserted.
- Every queue has a fixed capacity. The deep core does not allocate to absorb load.
- No recursion in the deep core.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is currently done in many places in the datastore where we tree-walk AVs and ATs.)

Comment thread docs/DEEP_CORE_STYLE.md

### Control flow

Prefer simple, explicit control flow. Split compound conditions into nested `if/else` rather than chaining them. State invariants positively. Avoid macros where a function will do.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this necessarily makes code simpler to read or more fault tolerant. It can often lead to more repetition and error prone code as a result.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur on the macro part, tho.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed on the macros part.

Comment thread docs/DEEP_CORE_STYLE.md

Prefer simple, explicit control flow. Split compound conditions into nested `if/else` rather than chaining them. State invariants positively. Avoid macros where a function will do.

### Naming
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this section can be cut. The standard Rust conventions should apply.

Comment thread docs/DEEP_CORE_STYLE.md

### Comments and formatting

- Comments explain *why*, not *what*. The code already says *what*.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "what"-paragraph-comments are also useful, as they can allow skipping sections of code by summarizing what they do. Why-comments are more valuable of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants